//
//  YZXWelcomeController.swift
//  YZXWeibo
//
//  Created by 叶周翔 on 16/7/22.
//  Copyright © 2016年 叶周翔. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage
class YZXWelcomeController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        self.setUpUI()
        
        YZXUserAccountVM.shareUserAccountVM.loadUserInfo { (error) -> () in
            if error != nil {
                print(error)
                return
            }
            // 设置头像
            self.setUpIcon()
        }
        
    }
    
    override func viewDidAppear(animated: Bool) {
        startAnimation()
    }
    //设置头像
    func setUpIcon(){
        if let icon = YZXUserAccountVM.shareUserAccountVM.userAccount?.avatar_large{
            let url = NSURL(string: icon)
            iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
        }
    }
    
    
    
    private func startAnimation(){
        // 重新设置约束 参照不能改变
        let offset = -(UIScreen.mainScreen().bounds.height-160)
        iconView.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(self.view).offset(offset)
        }
        
        UIView.animateWithDuration(1, delay: 0.2, usingSpringWithDamping: 0.75, initialSpringVelocity: 7, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                // 动画完成
                UIView.animateWithDuration(0.5, animations: { () -> Void in
                    self.weLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        // 切换到 mianTabBarVC
                        AppDelegate.jumpToOtherController(YZXTabBarController())
                })
        }
    }
    
    
    
    /**
     布局控件
     */
    private func setUpUI(){
        
        // 添加到父控件
        self.view.addSubview(bgImageView)
        self.view.addSubview(iconView)
        self.view.addSubview(weLabel)
        
        // 设置约束
        bgImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self.view)
            
        }
        
        
        
        iconView.snp_makeConstraints { (make) -> Void in
            
            make.bottom.equalTo(self.view).offset(-160)
            make.centerX.equalTo(self.view)
            make.height.width.equalTo(85)
            
        }
        
        weLabel.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(self.view)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
        
    }
    
    
    
    
    
    // MARK:-懒加载背景图片和按钮
    lazy var bgImageView:UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    lazy var iconView: UIImageView = {
        let icon = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        // 切圆角
        icon.layer.cornerRadius = icon.frame.size.width*0.5
        icon.clipsToBounds = true
        
        
        return icon
    }()
        
    lazy var weLabel: UILabel={
       let label = UILabel()
        label.text = "真TM烦"
        label.font = UIFont.systemFontOfSize(15)
        label.alpha = 0
        label.sizeToFit()
        return label
    }()
    
}
